//
//  SEGIntegralExchangeConfirmViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/6/1.
//

#import "SEGIntegralExchangeConfirmViewModel.h"

@implementation SEGIntegralExchangeConfirmViewModel

#pragma mark - business
- (void)getDefaultAddress:(void(^)(void))completed{
    [self.integralManager requestMyAddressFinished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            for (SEGAddressModel* object in self.integralManager.seg_segaddressmodel) {
                if ([object.defaultType isEqualToString:@"1"]) {
                    self.addressData = object;
                }
            }
        }
        completed();
    }];
}

- (void)getExchangeConfirmInfoWithIdString:(NSString*)idString completed:(void(^)(BOOL isSuccessed,NSString *errorMessage,BOOL shoulShowAddress,NSString* coins,NSInteger type))completed{
    [self.integralManager requestIntegralExchangeConfirmInfoWithId:idString finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        BOOL shoulShowAddress = false;
        if (isSuccessed) {
            self.confirmModel = self.integralManager.exchangeConfirmModel;
            if (self.confirmModel.type.integerValue == SEGIntegralProductType_OnlineLogistics || self.confirmModel.type.integerValue == SEGIntegralProductType_Property) {//线上物流发货/线下物业发货商品  显示地址
                shoulShowAddress = YES;
            }else {
                shoulShowAddress = NO;
            }
        }
        completed(isSuccessed,errorMessage,shoulShowAddress,[NSString stringWithFormat:@"应付：%@积分",self.confirmModel.coins],self.confirmModel.type.integerValue);
    }];
}

-(void) requestExchangeConfirmWithWithIdString:(NSString*)idString
                                         count:(NSUInteger)count
                                         phone:(NSString*)phone
                                       account:(NSString*)account
                                     completed:(void(^)(BOOL isSuccessed, NSString *errorMessage))completed{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:idString forKey:@"productId"];
    [params setObject:[NSString stringWithFormat:@"%ld",(unsigned long)count] forKey:@"quantity"];
    SEGIntegralProductType type = [self.confirmModel.type integerValue];
    switch (type) {
        case SEGIntegralProductType_OnlineLogistics:
        case SEGIntegralProductType_Property:
        {
            if (self.addressData.id.length > 0) {
                [params setObject:self.addressData.id forKey:@"addressId"];
            }else {
                [SEGAlertView showTmepMessageWithMessage:@"请选择收货地址"];
                return;
            }
        }
            break;
        case SEGIntegralProductType_OnlineExchangeCode:
        {
            if (phone.length != 11) {
                [SEGAlertView showTmepMessageWithMessage:kPhoneError];
                return;
            }else {
                [params setObject:phone forKey:@"tel"];
            }
        }
            break;
        case SEGIntegralProductType_OnlineVirtual:
        {
            if (account.length == 0) {
                [SEGAlertView showTmepMessageWithMessage:@"请输入充值手机号码或账号"];
                return;
            }else {
                [params setObject:account forKey:@"tel"];
            }
        }
            break;
    }
    [self.integralManager requestExchangeConfirmWithParams:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        completed(isSuccessed,errorMessage);
    }];
}

-(void)requestIntegralExchangeJudgeQuantityWithIdString:(NSString*)idString
                                                  count:(NSUInteger)count
                                              completed:(void(^)(BOOL isSuccessed, NSString *errorMessage, NSString *total))completed{
    [self.integralManager requestIntegralExchangeJudgeQuantityWithId:idString quantity:count finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSString* totalString = @"";
        if (isSuccessed) {
            NSUInteger total = [self.confirmModel.coins integerValue] * count;
            totalString = [NSString stringWithFormat:@"应付：%lu积分",(unsigned long)total];
        }
        completed(isSuccessed,errorMessage,totalString);
    }];
}


#pragma mark - getter
- (SEGAddressModel*)addressData
{
    if (!_addressData) {
        _addressData = [[SEGAddressModel alloc] init];
    }
    return _addressData;
}

-(SEGIntegralManager *)integralManager{
    if (!_integralManager) {
        _integralManager = [[SEGIntegralManager alloc] init];
    }
    return _integralManager;
}

- (SEGIntegralExchangeConfirmModel*)confirmModel
{
    if (!_confirmModel) {
        _confirmModel = [[SEGIntegralExchangeConfirmModel alloc] init];
        _confirmModel.type = @"1";
    }
    return _confirmModel;
}
@end
